<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-07-24 13:04:14
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\ProColor;
use App\Models\ProColorMy;

class ProColorMyRepository extends Repositories
    {
    public $relation_models = [];
    public function boot(ProColorMy $model)
        {
        $this->model = $model;
        }

    public function addMyColor($data, $flag = true)
        {
        //更新后还存在贸易色号
        if ($flag) {
            $this->updateOrCreate(['color_id' => $data->main_id, 'material_id' => $data->material_id],
                ['code_my' => $data->code_my, 'color_material_code' => $data->main_id . "_" . $data->material_id]);
            if (in_array($data->material_id, [2349, 2342])) {
                $material_id = $data->material_id == 2349 ? 2342 : 2349;
                $this->updateOrCreate(['color_id' => $data->main_id, 'material_id' => $material_id],
                    ['code_my' => $data->code_my, 'color_material_code' => $data->main_id . "_" . $material_id]);
                }

            }
        //更新后贸易色号没有了
        else {
            if (in_array($data->material_id, [2349, 2342])) {
                $material_id = $data->material_id == 2342 ? 2349 : 2342;
                $this->whereColor_id($data->main_id)->whereMaterial_id($material_id)->delete();
                }
            $this->whereColor_id($data->main_id)->whereMaterial_id($data->material_id)->delete();
            }
        //同步色卡颜色
        return ProColorRepository::run("sync_codemy", [$data->main_id]);
        }

    }